package com.example.work4.adapter.viewHolder

import android.content.Context
import android.os.Handler
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.R
import com.example.work4.databinding.ItemCommentBinding
import com.example.work4.http.MomentRetrofitImp
import com.example.work4.http.PersonalInfoRetrofitImp
import com.example.work4.http.body.CommentSaveBody
import com.example.work4.po.Comment
import com.example.work4.view.CommentDialog


class CommentViewHolder (
    var dataBinding: ItemCommentBinding, var momentId:Int, var comment:Comment, var handler: Handler? = null,
    var huifu:TextView = dataBinding.root.findViewById(R.id.static1),
    user1:TextView = dataBinding.root.findViewById(R.id.commentUsername),
    user2:TextView = dataBinding.root.findViewById(R.id.replyUsername),
) : RecyclerView.ViewHolder(dataBinding.root),View.OnClickListener {
    init {
        dataBinding.root.setOnClickListener(this)
        user1.setOnClickListener(this)
        user2.setOnClickListener(this)
    }
    var context: Context = super.itemView.context
    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.sendComment -> {
                val api = MomentRetrofitImp()
                if(handler!=null){
                    api.handler = handler;
                }
                api.context = context
                val body = CommentSaveBody()
                body.replyUserId = comment.userId
                body.momentId = this.momentId.toLong()
                body.content = CommentDialog.input?.text.toString();
                api.addComment(body)
            }
            R.id.commentUsername ->{
                val api = PersonalInfoRetrofitImp()
                api.context = this.context
                api.getPersonalInfo(comment.userId)
            }
            R.id.replyUsername->{
            val api = PersonalInfoRetrofitImp()
            api.context = this.context
            api.getPersonalInfo(comment.replyUserId)
            }
            else ->{
                CommentDialog.show(context,this)
            }
        }
    }
}